﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;

namespace Gameplay.PVE.Survivor
{
    public class DamageElement: ElementBase
    {
        public override void Execute()
        {
            if (config.sub_type == (int)EBattleElementSubType.DirectlyDead)
            {
                target.SetHpChange(-target.currentHp);
                HeroEventBubbleManager.Instance.RecevieDamage(target.unitId, target.currentHp,target.totalHp);
                if (target.isDead)
                {
                    HeroEventBubbleManager.Instance.Kill(source.unitId, target.unitId);
                }
                return;
            }
            var txttype = EFlyTextType.Damage;
            var value = 0;
            if (bullet != null && bullet.fixedDamage > 0)
            {
                value = bullet.fixedDamage;
            }
            else
            {
                value = Mathf.FloorToInt(SurvivorOutputFunction.GetOutputValue(config.id, source, target));
            }
           
            if (config.sub_type == (int)EBattleElementSubType.Heal)
            {
                txttype = EFlyTextType.Heal;
                target.SetHpChange(value);
                if (source != null)
                {
                    HeroEventBubbleManager.Instance.Cured(source.unitId, target.unitId, value, target.totalHp);
                }
            }
            else
            {
                target.lastHitTime = TimeManager.time;
                if (SurvivorManager.selfNoDamage && target.team == 0)
                {
                    return;
                }

                if (SurvivorManager.selfDamageUp && source is { team: 0 })
                {
                    value *= 50;
                }
                if (!SurvivorManager.isNoDamage)
                {
                    target.SetHpChange(-value);
                }
                if (target.team != 0)
                {
                    if (this.bullet != null)
                    {
                        if (this.bullet.isCritical)
                        {
                            txttype = EFlyTextType.CriticalDamage;
                        }
                    }
                }
                else
                {
                    txttype = EFlyTextType.OtherDamage;
                    if (this.bullet != null)
                    {
                        if (this.bullet.isCritical)
                        {
                            txttype = EFlyTextType.OtherCriticalDamage;
                        }
                    }
                }
                //表现
                target.OnHit();
                //计算怒气
                //target.SetMpChange(1);
                //source.SetMpChange(1);
                HeroEventBubbleManager.Instance.RecevieDamage(target.unitId, value,target.totalHp);
            }

            FlyTextManager.Instance.ShowFlyText(txttype, target.GetFlyTextPosition(), value.ToString());
            if (target.team == 2)
            {
                var distance = config.knockback_value;
                var time = Mathf.Sqrt(2 * distance / 10);
                target.SetKnockBack(config.knockback_value, time, target.Position - source.Position);
            }

            if (target.isDead && source != null)
            {
                HeroEventBubbleManager.Instance.Kill(source.unitId, target.unitId);
            }

            UnitManager.Instance.SetBattlePoint(target.position);
            if (source != null && bullet != null && config.sub_type != (int)EBattleElementSubType.Heal)
            {
                StatisticManager.Instance.AddDamage(source, bullet.sourceSpawner.damageType, value);
            }
        }

        public override void OnFree()
        {
            
        }

        public override bool IsImmediate()
        {
            return true;
        }
    }
}
